package edu.hebtu.controller;

import edu.hebtu.mapper.impl.AdminMapper;
import edu.hebtu.mapper.impl.UserMapper;
import edu.hebtu.pojo.Admin;
import edu.hebtu.pojo.User;
import edu.hebtu.util.IDCheck;
import edu.hebtu.util.PhoneCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProfileController {

    private final AdminMapper adminMapper;
    private final UserMapper userMapper;

    @Autowired
    public ProfileController(AdminMapper adminMapper, UserMapper userMapper) {
        this.adminMapper = adminMapper;
        this.userMapper = userMapper;
    }

    @RequestMapping("/editAdminProfile")
    public String editAdminProfile(Admin admin,
                                   String oldPwd, String newPwd, String newPwdRepeat,
                                   Model model, HttpSession session) {

        List<Admin> admins = adminMapper.findById(null);
        admins.removeIf(aValue -> aValue.getAdminId().trim().equals(admin.getAdminId().trim()));
        StringBuilder msg = new StringBuilder();
        boolean flag = false;
        if (admin.getName() == null || admin.getName().trim().equals("")) {
            msg.append("姓名不能为空！！！\n");
            flag = true;
        }
        if (admin.getCard() == null || admin.getCard().trim().equals("")) {
            msg.append("身份证号不能为空！！！");
            flag = true;
        } else if (!IDCheck.check(admin.getCard())) {
            msg.append("身份证号格式有误，请重新输入！！！(身份证号：").append(admin.getCard()).append(")\n");
            flag = true;
        } else {
            for (Admin admin1 : admins) {
                if (admin.getCard().trim().equals(admin1.getCard().trim())) {
                    msg.append("身份证号已存在！！！(身份证号：").append(admin.getCard()).append(")\n");
                    flag = true;
                }
            }
        }
        if (admin.getPhone() == null || admin.getPhone().trim().equals("")) {
            msg.append("手机号不能为空！！！");
            flag = true;
        } else if (!PhoneCheck.check(admin.getPhone().trim())) {
            msg.append("手机号格式有误，请重新输入！！！(手机号：").append(admin.getPhone()).append(")\n");
            flag = true;
        } else {
            for (Admin admin1 : admins) {
                if (admin.getPhone().trim().equals(admin1.getPhone().trim())) {
                    msg.append("手机号已存在！！！(手机号：").append(admin.getPhone()).append(")\n");
                    flag = true;
                }
            }
        }
        if (admin.getAddress() == null || admin.getAddress().equals("")) {
            msg.append("家庭住址不能为空！！！\n");
            flag = true;
        }

        if (oldPwd == null || oldPwd.trim().equals("")) {
            model.addAttribute("msgPwd", "请输入旧密码！！！");
        } else if (!adminMapper.findById(admin.getAdminId()).get(0).getPassword().trim().equals(oldPwd.trim())) {
            model.addAttribute("msgPwd", "旧密码错误！！！");
        } else if (newPwd == null || newPwd.trim().equals("")) {
            model.addAttribute("msgPwd", "请输入新密码！！！");
            model.addAttribute("oldPwd", oldPwd);
        } else if (newPwd.trim().length() < 6 || newPwd.trim().length() > 16) {
            model.addAttribute("msgPwd", "密码长度应在6~16之间！！！");
            model.addAttribute("oldPwd", oldPwd);
        } else if (!newPwd.trim().equals(newPwdRepeat.trim())) {
            model.addAttribute("msgPwd", "新密码不一致！！！");
            model.addAttribute("oldPwd", oldPwd);
            model.addAttribute("newPwd", newPwd);
            model.addAttribute("newPwdRepeat", newPwdRepeat);
        } else {
            Admin admin1 = adminMapper.findById(admin.getAdminId()).get(0);
            admin1.setPassword(newPwd);
            if (adminMapper.update(admin1) == 1){
                model.addAttribute("msgPwd", "密码修改成功！！！");
            }
        }

        if (flag) {
            model.addAttribute("msg", msg.toString());
            return "pages/pages-user-profile";
        }

        if (adminMapper.update(admin) == 1) {
            model.addAttribute("msg", "信息修改成功！！！");
            session.setAttribute("object", admin);
        }
        return "pages/pages-user-profile";
    }

    @RequestMapping("/editUserProfile")
    public String editUserProfile(User user,
                                  String oldPwd, String newPwd, String newPwdRepeat,
                                  Model model, HttpSession session) {

        List<User> users = userMapper.findById(null);
        users.removeIf(aValue -> aValue.getUserId().trim().equals(user.getUserId().trim()));
        StringBuilder msg = new StringBuilder();
        boolean flag = false;
        if (user.getName() == null || user.getName().trim().equals("")) {
            msg.append("姓名不能为空！！！\n");
            flag = true;
        }
        if (user.getCard() == null || user.getCard().trim().equals("")) {
            msg.append("身份证号不能为空！！！");
            flag = true;
        } else if (!IDCheck.check(user.getCard())) {
            msg.append("身份证号格式有误，请重新输入！！！(身份证号：").append(user.getCard()).append(")\n");
            flag = true;
        } else {
            for (User user1 : users) {
                if (user.getCard().trim().equals(user1.getCard().trim())) {
                    msg.append("身份证号已存在！！！(身份证号：").append(user.getCard()).append(")\n");
                    flag = true;
                }
            }
        }
        if (user.getPhone() == null || user.getPhone().trim().equals("")) {
            msg.append("手机号不能为空！！！");
            flag = true;
        } else if (!PhoneCheck.check(user.getPhone().trim())) {
            msg.append("手机号格式有误，请重新输入！！！(手机号：").append(user.getPhone()).append(")\n");
            flag = true;
        } else {
            for (User user1 : users) {
                if (user.getPhone().trim().equals(user1.getPhone().trim())) {
                    msg.append("手机号已存在！！！(手机号：").append(user.getPhone()).append(")\n");
                    flag = true;
                }
            }
        }

        if (oldPwd == null || oldPwd.trim().equals("")) {
            model.addAttribute("msgPwd", "请输入旧密码！！！");
        } else if (!userMapper.findById(user.getUserId()).get(0).getPassword().trim().equals(oldPwd.trim())) {
            model.addAttribute("msgPwd", "旧密码错误！！！");
        } else if (newPwd == null || newPwd.trim().equals("")) {
            model.addAttribute("msgPwd", "请输入新密码！！！");
            model.addAttribute("oldPwd", oldPwd);
        } else if (newPwd.trim().length() < 6 || newPwd.trim().length() > 16) {
            model.addAttribute("msgPwd", "密码长度应在6~16之间！！！");
            model.addAttribute("oldPwd", oldPwd);
        } else if (!newPwd.trim().equals(newPwdRepeat.trim())) {
            model.addAttribute("msgPwd", "新密码不一致！！！");
            model.addAttribute("oldPwd", oldPwd);
            model.addAttribute("newPwd", newPwd);
            model.addAttribute("newPwdRepeat", newPwdRepeat);
        } else {
            User user1 = userMapper.findById(user.getUserId()).get(0);
            user1.setPassword(newPwd);
            if (userMapper.update(user1) == 1){
                model.addAttribute("msgPwd", "密码修改成功！！！");
            }
        }

        if (flag) {
            model.addAttribute("msg", msg.toString());
            return "pages/pages-user-profile";
        }

        if (userMapper.update(user) == 1) {
            model.addAttribute("msg", "信息修改成功！！！");
            session.setAttribute("user", user);
        }
        return "pages/pages-user-profile";
    }

}
